package com.csap.framework.oauth2.server.service.impl;

import com.csap.framework.oauth2.server.entity.ScopeEntity;
import com.csap.framework.oauth2.server.entity.ScopePermissionsEntity;
import com.csap.framework.oauth2.server.service.ClientScopeService;
import com.csap.framework.annotation.apidoc.Description;
import com.csap.framework.oauth2.server.service.ScopePermissionsService;
import com.csap.framework.oauth2.server.service.ScopeService;
import com.csap.mybatisplus.extension.service.impl.BaseCsapServiceImpl;
import com.csap.framework.oauth2.server.model.ClientScopeModel;
import org.springframework.stereotype.Service;
import com.csap.framework.oauth2.server.mybatis.ClientScopeMapper;
import com.csap.framework.oauth2.server.entity.ClientScopeEntity;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * 客户端作用域
 * </p>
 *
 * @author yangchengfu
 * @dateTime 2021-07-25 23:51:30
 */
@Service
@Description(value = "客户端作用域ServiceImpl")
public class ClientScopeServiceImpl extends BaseCsapServiceImpl<ClientScopeMapper, ClientScopeEntity, ClientScopeModel, Long> implements ClientScopeService {
    @Resource
    private ScopeService scopeService;
    @Resource
    private ScopePermissionsService scopePermissionsService;

    @Override
    public Set<String> permissionsList(String clientId) {
        return lambdaQuery()
                .select(ClientScopeEntity::getScopeId)
                .eq(ClientScopeEntity::getClientId, clientId)
                .listField(Long.class)
                .orElse(Collections.emptyList())
                .stream()
                .flatMap(i -> scopePermissionsService.permissions(i).stream())
                .collect(Collectors.toSet());
    }

    @Override
    public List<String> scopeList(String clientId) {
        return scopeService.lambdaQuery()
                .select(ScopeEntity::getName)
                .inner(ScopeEntity::getScopeId, ClientScopeEntity::getScopeId)
                .targetWhere()
                .eq(ClientScopeEntity::getClientId, clientId)
                .end()
                .end()
                .listField(String.class)
                .get();
    }
}
